// pages/message/message.js
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    personalinfo: {
      // sex: '1', // 男
      email: '',
      // currentStatus:'0' // 在读
    },
    disabled: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.findByUserId()
  },
  // 判断用户信息是否完善
  findByUserId: function () {
var that = this;
    wx.request({
      url: app.globalData.url + 'findByUserId?userId=' + wx.getStorageSync('userId'),
      header: {},
      success: function (res) {
        console.log(res)
        var personalinfo = res.data.body;
        that.setData({
          personalinfo: personalinfo
        })
      },
      fail: function (res) { },
      complete: function (res) { },
    })
  },
  // 获取姓名
  getname: function (e) {
    var personalinfo = this.data.personalinfo;
    personalinfo.name = e.detail.value
    this.setData({
      personalinfo: personalinfo
    })
  },
  // 获取性别
  sexChange: function (e) {
    var personalinfo = this.data.personalinfo;
    personalinfo.sex = e.detail.value
    this.setData({
      personalinfo: personalinfo
    })
  },
  // 获取身份证号码
  getidCard: function (e) {
    var idCard = e.detail.value
    if (idCard == '') {
      wx.showToast({
        title: '身份证不能为空',
        image: '../img/nomore.png',
        duration: 2000
      })
    } else {
      // 身份证验证
      var isId = this.IdentityCodeValid(idCard)
      var personalinfo = this.data.personalinfo
      personalinfo.idCard = idCard
      this.setData({
        personalinfo: personalinfo
      })
    }
  },
  // 是否在读
  isSchoolChange: function (e) {
    var personalinfo = this.data.personalinfo;
    personalinfo.currentStatus = e.detail.value
    this.setData({
      personalinfo: personalinfo
    })
  },
  // 学校
  getCollege: function (e) {
    var personalinfo = this.data.personalinfo;
    personalinfo.college = e.detail.value
    this.setData({
      personalinfo: personalinfo
    })
  },
  // 邮箱
  getEmail: function (e) {
    var personalinfo = this.data.personalinfo;
    personalinfo.email = e.detail.value
    if (personalinfo.email.length > 0) {
      var isEmail = this.isEmail(personalinfo.email)
      if (isEmail != true) {
        wx.showToast({
          title: '邮箱格式有误',
          image: '../img/nomore.png',
          duration: 2000,
          mask: true
        })
      }
    }
    this.setData({
      personalinfo: personalinfo
    })
    console.log(isEmail)
  },
  // QQ
  getQQ: function (e) {
    var personalinfo = this.data.personalinfo;
    personalinfo.qq = e.detail.value
    this.setData({
      personalinfo: personalinfo
    })
    console.log(personalinfo)
  },
  // 手机号
  getmobile: function (e) {
    var personalinfo = this.data.personalinfo
    personalinfo.telNumber = e.detail.value;
    var isMobile = this.isMobile(personalinfo.telNumber)
    this.setData({
      personalinfo: personalinfo
    })
  },
  // 验证码
  getVerificationCode: function (e) {
    var personalinfo = this.data.personalinfo;
    personalinfo.ver_code = e.detail.value
    this.setData({
      personalinfo: personalinfo
    })
  },
  // 个人简介
  getRemark: function (e) {
    var personalinfo = this.data.personalinfo;
    personalinfo.remarks = e.detail.value
    this.setData({
      personalinfo: personalinfo
    })
  },
  // 完善修改个人信息
  summit: function () {
    var that = this;
    var personalinfo = this.data.personalinfo;
    var isId = this.IdentityCodeValid(personalinfo.idCard)
    if (!personalinfo.name || personalinfo.name == '') {
      wx.showToast({
        title: '姓名不能为空',
        image: '../img/nomore.png',
        duration: 1500,
      })
    } else if (!personalinfo.sex || personalinfo.sex == '') {
      wx.showToast({
        title: '性别不能为空',
        image: '../img/nomore.png',
        duration: 1500,
      })
    } else if (isId == true) {
      var isEmail = this.isEmail(personalinfo.email)
      if (personalinfo.email.length > 0 && isEmail != true) {
        wx.showToast({
          title: '邮箱格式有误',
          image: '../img/nomore.png',
          duration: 2000,
          mask: true
        })
      } else {
        var isMobile = this.isMobile(personalinfo.telNumber)
        console.log(personalinfo)
        if (isMobile == true) {
          // 验证通过，提交信息
          that.setData({
            disable:true
          })
          wx.request({
            //url: app.globalData.url + 'editMsg?userId=' + wx.getStorageSync('userId'),
            url: app.globalData.url + 'editMsg?',
            header: {
              "Content-Type": "application/x-www-form-urlencoded"  
            },
            method:'POST',
            data: personalinfo,
            success: function (res) {
              console.log(res)
              if(res.data.success){
                wx.navigateTo({
                  url: '/pages/success/success?type=signup',
                })
                  // wx.showToast({
                  //   title: '提交成功',
                  //   icon: 'success',
                  //   duration: 1500,
                  //   mask: true,
                  //   success: function(res) {
                  //     setTimeout(function () {
                  //       wx.navigateBack({
                  //         delta: 1,
                  //       })
                  //     }, 1500)
                  //   },
                  // })
              }else{
                wx.showModal({
                  title: '提示',
                  content: res.data.msg,
                  showCancel: false,
                  success:function(){
                    setTimeout(function(){
                      that.setData({
                        disable: false
                      })
                    },1500)
                  }
                })
              }
            },
            fail: function (res) {
              that.setData({
                disable: false
              })
             },
            complete: function (res) { },
          })
        }
      }
    }
    console.log(personalinfo)
  },
  // 发送验证码
  sendmsg: function () {
    var that = this;
    that.setData({
      disabled: true,
      vertime: 60
    })
    var vertime = 60;
    var timer = setInterval(function () {
      vertime--;
      if (vertime == 0) {
        clearInterval(timer);
        that.setData({
          disabled: false,
        })
      } else {
        that.setData({
          disabled: true,
          vertime: vertime
        })
      }
    }, 1000)

  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },
  // 身份证验证
  IdentityCodeValid: function (code) {
    var city = { 11: "北京", 12: "天津", 13: "河北", 14: "山西", 15: "内蒙古", 21: "辽宁", 22: "吉林", 23: "黑龙江 ", 31: "上海", 32: "江苏", 33: "浙江", 34: "安徽", 35: "福建", 36: "江西", 37: "山东", 41: "河南", 42: "湖北 ", 43: "湖南", 44: "广东", 45: "广西", 46: "海南", 50: "重庆", 51: "四川", 52: "贵州", 53: "云南", 54: "西藏 ", 61: "陕西", 62: "甘肃", 63: "青海", 64: "宁夏", 65: "新疆", 71: "台湾", 81: "香港", 82: "澳门", 91: "国外 " };
    var tip = "";
    var pass = true;

    if (!code || !/^\d{6}(18|19|20)?\d{2}(0[1-9]|1[12])(0[1-9]|[12]\d|3[01])\d{3}(\d|X)$/i.test(code)) {
      tip = "身份证号格式错误";
      pass = false;
    }

    else if (!city[code.substr(0, 2)]) {
      tip = "地址编码错误";
      pass = false;
    }
    else {
      //18位身份证需要验证最后一位校验位
      if (code.length == 18) {
        code = code.split('');
        //∑(ai×Wi)(mod 11)
        //加权因子
        var factor = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2];
        //校验位
        var parity = [1, 0, 'X', 9, 8, 7, 6, 5, 4, 3, 2];
        var sum = 0;
        var ai = 0;
        var wi = 0;
        for (var i = 0; i < 17; i++) {
          ai = code[i];
          wi = factor[i];
          sum += ai * wi;
        }
        var last = parity[sum % 11];
        if (parity[sum % 11] != code[17]) {
          tip = "校验位错误";
          pass = false;
        }
      }
    }
    if (!pass) {
      wx.showToast({
        title: tip,
        image: '../img/nomore.png',
        duration: 2000,
        mask: true,
      })
    }
    return pass;
  },
  // 邮箱验证
  isEmail: function (str) {
    var reg = /^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+(.[a-zA-Z0-9_-])+/;
    return reg.test(str);
  },
  // 手机号验证
  isMobile: function (phone) {
    var pass = false;
    if (!phone) {
      phone = ''
    }
    if (phone == '') {
      wx.showToast({
        title: '手机号不能为空',
        image: '../img/nomore.png',
        duration: 2000
      })
    } else if (phone.length < 11) {
      wx.showToast({
        title: '手机号码有误',
        image: '../img/nomore.png',
        duration: 2000
      })
    } else if (!(/^1[34578]\d{9}$/.test(phone))) {
      wx.showToast({
        title: '手机号码有误',
        image: '../img/nomore.png',
        duration: 2000
      })
    } else {
      pass = true;
    }
    return pass;
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})